package org.example;

import javax.swing.*;
import javax.swing.border.*;
import java.awt.*;
import java.awt.event.*;

public class LoginFrame extends JFrame {
    private JTextField usernameField;
    private JPasswordField passwordField;

    public LoginFrame() {
        setTitle("学生管理系统 - 登录");
        setSize(400, 300);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLocationRelativeTo(null);
        setResizable(false);

        // 设置窗口图标
        ImageIcon icon = new ImageIcon("icon.png");  // 需要添加一个icon.png文件
        setIconImage(icon.getImage());

        // 创建主面板
        JPanel mainPanel = new JPanel();
        mainPanel.setLayout(new BorderLayout());
        mainPanel.setBorder(new EmptyBorder(20, 40, 20, 40));

        // 创建标题面板
        JPanel titlePanel = new JPanel();
        JLabel titleLabel = new JLabel("学生管理系统");
        titleLabel.setFont(new Font("微软雅黑", Font.BOLD, 24));
        titleLabel.setForeground(new Color(51, 51, 51));
        titlePanel.add(titleLabel);

        // 创建输入面板
        JPanel inputPanel = new JPanel();
        inputPanel.setLayout(new GridLayout(2, 1, 0, 20));
        inputPanel.setBorder(new EmptyBorder(20, 0, 20, 0));

        // 用户名面板
        JPanel usernamePanel = new JPanel(new BorderLayout(10, 0));
        JLabel userIcon = new JLabel(new ImageIcon("user.png"));  // 需要添加user.png
        usernameField = new JTextField();
        usernameField.setPreferredSize(new Dimension(200, 30));
        usernamePanel.add(userIcon, BorderLayout.WEST);
        usernamePanel.add(usernameField, BorderLayout.CENTER);

        // 密码面板
        JPanel passwordPanel = new JPanel(new BorderLayout(10, 0));
        JLabel passIcon = new JLabel(new ImageIcon("password.png"));  // 需要添加password.png
        passwordField = new JPasswordField();
        passwordField.setPreferredSize(new Dimension(200, 30));
        passwordPanel.add(passIcon, BorderLayout.WEST);
        passwordPanel.add(passwordField, BorderLayout.CENTER);

        inputPanel.add(usernamePanel);
        inputPanel.add(passwordPanel);

        // 按钮面板
        JPanel buttonPanel = new JPanel(new FlowLayout(FlowLayout.CENTER, 20, 0));

        JButton loginButton = new JButton("登录");
        JButton cancelButton = new JButton("取消");

        // 美化按钮
        styleButton(loginButton, new Color(65, 105, 225));
        styleButton(cancelButton, new Color(100, 100, 100));

        buttonPanel.add(loginButton);
        buttonPanel.add(cancelButton);

        // 组装主面板
        mainPanel.add(titlePanel, BorderLayout.NORTH);
        mainPanel.add(inputPanel, BorderLayout.CENTER);
        mainPanel.add(buttonPanel, BorderLayout.SOUTH);

        // 添加主面板到窗口
        add(mainPanel);

        // 添加按钮事件
        loginButton.addActionListener(e -> login());
        cancelButton.addActionListener(e -> System.exit(0));

        // 添加回车键登录
        getRootPane().setDefaultButton(loginButton);
    }

    private void styleButton(JButton button, Color color) {
        button.setPreferredSize(new Dimension(100, 35));
        button.setBackground(color);
        button.setForeground(Color.WHITE);
        button.setFocusPainted(false);
        button.setBorderPainted(false);
        button.setFont(new Font("微软雅黑", Font.BOLD, 14));

        // 添加鼠标悬停效果
        button.addMouseListener(new MouseAdapter() {
            public void mouseEntered(MouseEvent e) {
                button.setBackground(color.brighter());
            }
            public void mouseExited(MouseEvent e) {
                button.setBackground(color);
            }
        });
    }

    private void login() {
        String username = usernameField.getText().trim();
        String password = new String(passwordField.getPassword());

        if (username.isEmpty() || password.isEmpty()) {
            JOptionPane.showMessageDialog(this,
                "用户名和密码不能为空！",
                "提示",
                JOptionPane.WARNING_MESSAGE);
            return;
        }

        // 验证登录
        if (username.equals("admin") && password.equals("123456")) {
            new StudentManagementFrame().setVisible(true);
            this.dispose();
        } else {
            JOptionPane.showMessageDialog(this,
                "用户名或密码错误！",
                "错误",
                JOptionPane.ERROR_MESSAGE);
            passwordField.setText("");
        }
    }

    public static void main(String[] args) {
        try {
            // 设置观感
            UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
        } catch (Exception e) {
            e.printStackTrace();
        }

        SwingUtilities.invokeLater(() -> {
            new LoginFrame().setVisible(true);
        });
    }
}
